package com.oneinlet.service;

import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.List;
import java.util.Optional;

/**
 * Created by WangZiHe on 2017/11/13
 * QQ/WeChat:648830605
 * QQ-Group:368512253
 * Blog:www.520code.net
 * Github:https://github.com/yancheng199287
 */

public abstract class BaseService<T, ID extends Serializable> implements IBaseService<T, ID> {
    protected abstract JpaRepository<T, ID> getBaseDAO();

    public List<T> findAll() {
        return getBaseDAO().findAll();
    }

    public List<T> findAll(@NotNull Sort sort) {
        return getBaseDAO().findAll(sort);
    }

    public List<T> findAllById(Iterable<ID> iterable) {
        return getBaseDAO().findAllById(iterable);
    }

    public Page<T> findAll(@NotNull Pageable pageable) {
        return getBaseDAO().findAll(pageable);
    }

    public <S extends T> Page<S> findAll(@NotNull Example<S> example, @NotNull Pageable pageble) {
        return getBaseDAO().findAll(example, pageble);
    }

    public <S extends T> List<S> findAll(@NotNull Example<S> example) {
        return getBaseDAO().findAll(example);
    }

    @NotNull
    public <S extends T> List<S> findAll(@NotNull Example<S> example, @NotNull Sort sort) {
        return getBaseDAO().findAll(example, sort);
    }

    public <S extends T> S save(S s) {
        return getBaseDAO().save(s);
    }

    @Override
    public <S extends T> List<S> save(Iterable<S> paramIterable) {
        return getBaseDAO().saveAll(paramIterable);
    }

    public void flush() {
        getBaseDAO().flush();
    }

    public <S extends T> S saveAndFlush(S s) {
        return getBaseDAO().saveAndFlush(s);
    }

    public void deleteById(ID id) {
        getBaseDAO().deleteById(id);
    }

    public void delete(T t) {
        getBaseDAO().delete(t);
    }

    public void deleteInBatch(@NotNull Iterable<T> iterable) {
        getBaseDAO().deleteInBatch(iterable);
    }

    public void deleteAllInBatch() {
        getBaseDAO().deleteAllInBatch();
    }

    public T getOne(@NotNull ID id) {
        return getBaseDAO().getOne(id);
    }

    public Optional<T> findOne(@NotNull ID id) {
        return getBaseDAO().findById(id);
    }

    public <S extends T> Optional<S> findOne(@NotNull Example<S> example) {
        return getBaseDAO().findOne(example);
    }

    public <S extends T> long count(@NotNull Example<S> example) {
        return getBaseDAO().count();
    }

    public <S extends T> boolean exists(@NotNull Example<S> example) {
        return getBaseDAO().exists(example);
    }
}
